'use strict';

const Controller = require('egg/index').Controller;

class ElasticsearchController extends Controller {

  async count() {
    let result = await this.app.elasticsearch.count({
      index: 'news',
      // type: 'doc',
      body: {
        query: {
          match: {
            content: 'add'
          }
        }
      }
    });
    this.ctx.sendSuccess(result)
  }

  async analyze(ctx) {
    this.ctx.validate({
      text: {
        type: 'string',
      },
      analyzer: {
        type: 'string',
      },
    });
    const res = await ctx.$wPost('http://localhost/elasticsearch/_analyze', ctx.w_body('text analyzer'), {
      headers: {
        'Content-Type': 'application/json; charset=UTF-8'
      }
    })
    if (res.data && res.data.tokens) {
      ctx.sendSuccess(res.data.tokens)
    } else {
      throw new Error('没查到数据')
    }
  }
}

module.exports = ElasticsearchController;
